#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
#include <vector>
#include <sstream>
#include "timer.h"
#include "dot.h"
#include "bullet.h"
#include "constant.h"
#include "globals.h"

bool load_files()
{
    //Load the dot image
    dot = load_image( "res/plane.png" );
    bullet = load_image( "res/bullet.png" );
    startSurf = load_image( "res/start.png" );

    //Open the font
    font = TTF_OpenFont( "res/lazy.ttf", 20 );

    //If there was a problem in loading the dot
    if( dot == NULL )
    {
        return false;
    }

    //If there was an error in loading the font
    if( font == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

int main( int argc, char* args[] )
{
    srand(SDL_GetTicks());

    //Quit flag
    bool quit = false;
    int status = 0;
    //bool collide = false;

    //The dot that will be used
    Dot myDot;
    std::vector<Bullet> myBullet(30);

    //The frame rate regulator
    Timer fps;
    Timer survive;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }

    //While the user hasn't quit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();

        if (status == 0)
        {
            while( SDL_PollEvent( &event ) )
            {
                //If a key was pressed
                if( event.type == SDL_KEYDOWN )
                {
                    //If enter was pressed
                    if( event.key.keysym.sym == SDLK_RETURN )
                    {
                        survive.start();
                        status = 1;

                        myDot.reset();
                        for (int i=0; i<myBullet.size(); ++i)
                        {
                            int x, y;
                            int xVel = 0, yVel = 0;

                            if (rand() % 4 == 0)
                            {
                                x = 0;
                                y = rand() % (SCREEN_HEIGHT + 1);
                                xVel = rand() % 2 + 1;
                                yVel = rand() % 5 - 2;
                            }
                            else if (rand() % 4 == 1)
                            {
                                x = SCREEN_WIDTH;
                                y = rand() % (SCREEN_HEIGHT + 1);
                                xVel = rand() % 2 - 2;
                                yVel = rand() % 5 - 2;
                            }
                            else if (rand() % 4 == 2)
                            {
                                x = rand() % (SCREEN_WIDTH + 1);
                                y = 0;
                                xVel = rand() % 5 - 2;
                                yVel = rand() % 2 + 1;
                            }
                            else
                            {
                                x = rand() % (SCREEN_WIDTH + 1);
                                y = SCREEN_HEIGHT;
                                xVel = rand() % 5 - 2;
                                yVel = rand() % 2 - 2;
                            }

                            myBullet[i].set(x, y, xVel, yVel);
                        }
                    }
                }

                //If the user has Xed out the window
                if( event.type == SDL_QUIT )
                {
                    //Quit the program
                    quit = true;
                }
            }

            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) );
            apply_surface( 0, 0, startSurf, screen );
        }
        else if (status == 1)
        {
            //While there's events to handle
            while( SDL_PollEvent( &event ) )
            {
                //Handle events for the dot
                myDot.handle_input();

                //If the user has Xed out the window
                if( event.type == SDL_QUIT )
                {
                    //Quit the program
                    quit = true;
                }
            }

            //Move the dot
            myDot.move();
            for (int i=0; i<myBullet.size(); ++i)
            {
                myBullet[i].move();
                if ( check_collision(myDot.getBox(), myBullet[i].getBox()) )
                {
                    std::stringstream words;
                    words << "You insisted " << ( survive.get_ticks() / 1000.f ) << " seconds.";
                    message = TTF_RenderText_Solid( font, words.str().c_str(), textColor );
                    apply_surface( ( SCREEN_WIDTH - message->w ) / 2, ( SCREEN_HEIGHT - message->h ) / 2, message, screen );
                    status = 2;
                    goto FLAG;

                    //SDL_WM_SetCaption( "Detected Collision!!!", NULL );
                    //collide = true;
                }
            }

            //Fill the screen white
            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x0, 0x0, 0x0 ) );

            //Show the dot on the screen
            myDot.show();
            for (int i=0; i<myBullet.size(); ++i)
                myBullet[i].show();
        }
        else if (status == 2)
        {
            while( SDL_PollEvent( &event ) )
            {
                //If a key was pressed
                if( event.type == SDL_KEYDOWN )
                {
                    //If enter was pressed
                    if( event.key.keysym.sym == SDLK_RETURN )
                    {
                        status = 0;
                    }
                }

                //If the user has Xed out the window
                if( event.type == SDL_QUIT )
                {
                    //Quit the program
                    quit = true;
                }
            }
        }

FLAG:
        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }

        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}
